package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TbProductCategory;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.TbProduct;
import com.ruoyi.system.service.ITbProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品信息 ：与店铺关联Controller
 * 
 * @author ruoyi
 * @date 2024-09-09
 * ...0...1...2
 *
 */
@RestController
@RequestMapping("/product")
public class TbProductController extends BaseController
{
    @Autowired
    private ITbProductService tbProductService;

    /**
     * 查询商品信息 ：与店铺关联列表
     */
    @RequiresPermissions("system:product:list")
    @GetMapping("/list")
    public TableDataInfo list(TbProduct tbProduct)
    {

        startPage();
        List<TbProduct> list = tbProductService.selectTbProductList(tbProduct);
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出商品信息 ：与店铺关联列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "商品信息 ：与店铺关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbProduct tbProduct)
    {
        List<TbProduct> list = tbProductService.selectTbProductList(tbProduct);
        ExcelUtil<TbProduct> util = new ExcelUtil<TbProduct>(TbProduct.class);
        util.exportExcel(response, list, "商品信息 ：与店铺关联数据");
    }

    /**
     * 获取商品信息 ：与店铺关联详细信息
     */
    @RequiresPermissions("system:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(tbProductService.selectTbProductByProductId(productId));
    }

    /**
     * 新增商品信息 ：与店铺关联
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品信息 ：与店铺关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProduct tbProduct)
    {
        int row = 0;
        int rows = tbProductService.insertTbProduct(tbProduct);
        if (rows > 0){
            row = tbProductService.insertTbProductInventory(tbProduct);
            System.out.println("添加库存影响行数：" + row);
        }
        if (row == 1){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
//        return toAjax(tbProductService.insertTbProduct(tbProduct));
    }

    /**
     * 修改商品信息 ：与店铺关联
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品信息 ：与店铺关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProduct tbProduct)
    {
        return toAjax(tbProductService.updateTbProduct(tbProduct));
    }

    /**
     * 删除商品信息 ：与店铺关联
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "商品信息 ：与店铺关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}/{storeId}")
    public AjaxResult remove(@PathVariable Long[] productIds,@PathVariable Long storeId)
    {
//        System.out.println("Pid = " + Arrays.toString(productIds) + " / " + "Sid = " + storeId);
        int inventoryRow = 0;
        int rows = tbProductService.deleteTbProductByProductIds(productIds);
//        System.out.println("111111111111111111111111");
//        System.out.println("rows = " + rows);
        if (rows > 0){
            inventoryRow = tbProductService.deleteTbProductInventoryByPidAndSid(productIds,storeId);
        }
        if (inventoryRow != 0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
//        return toAjax(tbProductService.deleteTbProductByProductIds(productIds));
    }

    /**
     * 查询商品类别字典
     */
    @RequiresPermissions("system:product:diction")
    @GetMapping("/diction")
    public TableDataInfo selectDiction()
    {
        List<TbProductCategory> list = tbProductService.selectTbProductCategoryList();
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 新增商品库存信息 ：与店铺关联
     */
    @RequiresPermissions("system:product:inventory")
    @Log(title = "商品库存信息" , businessType = BusinessType.INSERT)
    @PostMapping("/inventory")
    public AjaxResult addInventory(@RequestBody TbProduct tbProduct){
//        System.out.println("11111111111111111111");
        return toAjax(tbProductService.insertTbProductInventory(tbProduct));
    }

}
